package com.xmb.wechat.view.qq;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.TimePickerView;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.nil.sdk.utils.DateUtils;
import com.xmb.wechat.R;
import com.xmb.wechat.R2;
import com.xmb.wechat.base.BaseActivity;
import com.xmb.wechat.bean.BankCardBean;
import com.xmb.wechat.definterface.HintListener;
import com.xmb.wechat.entity.QQWithdrawEntity;
import com.xmb.wechat.util.MoneyFormat;
import com.xmb.wechat.view.wechat.money.WechatFundBankCardActivity;

import java.util.Random;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author lijiangop
 * @CreateTime 2020/5/21 09:33
 */
public class QQWithdrawActivity extends BaseActivity {

    private static final int REQUEST_BANK_CARD = 007;

    @BindView(R2.id.tv_bank_card)
    TextView mTvBankCard;
    @BindView(R2.id.et_money)
    EditText mEtMoney;
    @BindView(R2.id.tv_received)
    TextView mTvReceived;
    @BindView(R2.id.tv_processing)
    TextView mTvProcessing;
    @BindView(R2.id.tv_time)
    TextView mTvTime;
    @BindView(R2.id.et_order_num)
    EditText mEtOrderNum;

    private QQWithdrawEntity mEntity;

    public QQWithdrawActivity() {
        super(R.layout.activity_qq_withdraw);
    }

    @Override
    protected void initView() {
        mEntity = new QQWithdrawEntity();
        setWithDrawState(true);
        showExemption(new HintListener() {
            @Override
            public void onConfirm() {
                //do nothing
            }

            @Override
            public void onCancel() {
                QQWithdrawActivity.this.finish();
            }
        });
    }

    @OnClick({R2.id.re_bank_card, R2.id.tv_received, R2.id.tv_processing, R2.id.ll_time, R2.id.btn_preview})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.re_bank_card) {
            WechatFundBankCardActivity.startforResult(this, REQUEST_BANK_CARD);
        } else if (id == R.id.tv_received) {
            setWithDrawState(true);
        } else if (id == R.id.tv_processing) {
            setWithDrawState(false);
        } else if (id == R.id.ll_time) {
            KeyboardUtils.hideSoftInput(this);
            @SuppressLint("SimpleDateFormat")
            TimePickerView pvTime = new TimePickerBuilder(this, (date, v) -> {//选中事件回调
                String time = DateUtils.getStringByFormat(date, "yyyy-MM-dd HH:mm");
                mEntity.setTime(time);
                mTvTime.setText(time);
            }).setType(new boolean[]{false, true, true, true, true, false})// 默认全部显示
                    .setCancelText("取消")//取消按钮文字
                    .setSubmitText("确定")//确认按钮文字
                    .setTitleSize(20)//标题文字大小
                    .setTitleText("时间")//标题文字
                    .setOutSideCancelable(true)//点击屏幕，点在控件外部范围时，是否取消显示
                    .isCyclic(true)//是否循环滚动
                    .setSubmitColor(getResources().getColor(R.color.wechat_tab_bg_green))//确定按钮文字颜色
                    .setCancelColor(getResources().getColor(R.color.wechat_tab_bg_green))//取消按钮文字颜色
                    .setLabel("年", "月", "日", "时", "分", "秒")//默认设置为年月日时分秒
                    .isCenterLabel(true) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                    .build();
            pvTime.show();
        } else if (id == R.id.btn_preview) {
            if (TextUtils.isEmpty(mEntity.getBankCard())) {
                ToastUtils.showShort("请选择银行卡");
                return;
            }
            String money = mEtMoney.getText().toString().trim();
            if (TextUtils.isEmpty(money)) {
                ToastUtils.showShort("请填写提现金额");
                return;
            }
            money = parseMoney(money);
            mEntity.setMoney(money);
            String serviceCharge = getServiceCharge(money);
            mEntity.setServiceCharge(serviceCharge);
            mEntity.setRealDeduct(getRealDeduct(money, serviceCharge));
            if (TextUtils.isEmpty(mEntity.getTime())) {
                ToastUtils.showShort("请选择交易时间");
                return;
            }
            String orderNum = mEtOrderNum.getText().toString().trim();
            orderNum = TextUtils.isEmpty(orderNum) ? getRandomOrderNum(mEntity.isReceived()) : orderNum;
            mEntity.setOrderNum(orderNum);
            QQWithdrawDetailActivity.start(this, mEntity);
        }
    }

    private String getRealDeduct(String money, String serviceCharge) {
        try {
            double v = Double.parseDouble(money);
            double v1;
            if (TextUtils.isEmpty(serviceCharge))
                v1 = 0;
            else
                v1 = Double.parseDouble(serviceCharge);
            return MoneyFormat.formatMoney(v + v1);
        } catch (Exception e) {
            ToastUtils.showShort("金额输入有误");
            e.printStackTrace();
        }
        return "";
    }

    private String getServiceCharge(String money) {
        if (TextUtils.isEmpty(money))
            return "";
        try {
            double v = Double.parseDouble(money);
            if (v <= 0.1)
                return "";
            double serviceCharge = v * 0.001;
            if (serviceCharge < 0.1)
                serviceCharge = 0.1;
            return MoneyFormat.formatMoney(serviceCharge);
        } catch (Exception e) {
            ToastUtils.showShort("金额输入有误");
            e.printStackTrace();
        }
        return "";
    }

    private void setWithDrawState(boolean isReceived) {
        mTvReceived.setSelected(isReceived);
        mTvProcessing.setSelected(!isReceived);
        mEntity.setReceived(isReceived);
    }

    private String parseMoney(String money) {
        try {
            return MoneyFormat.formatMoney(money);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    private String getRandomOrderNum(boolean received) {
        if (received)
            return "443" + DateUtils.getCurDateByFormat("yyyyMMdd") +
                    new Random().nextInt(10) +
                    new Random().nextInt(10) +
                    new Random().nextInt(10) +
                    new Random().nextInt(10) +
                    new Random().nextInt(10) +
                    new Random().nextInt(10) +
                    new Random().nextInt(10) +
                    new Random().nextInt(10) +
                    new Random().nextInt(10) +
                    new Random().nextInt(10);
        else
            return DateUtils.getCurDateByFormat("yyyyMMdd") + "2000400110" +
                    new Random().nextInt(10) +
                    new Random().nextInt(10) +
                    new Random().nextInt(10) +
                    new Random().nextInt(10) +
                    new Random().nextInt(10) +
                    new Random().nextInt(10) +
                    new Random().nextInt(10) +
                    new Random().nextInt(10) +
                    new Random().nextInt(10) +
                    new Random().nextInt(10) +
                    new Random().nextInt(10) +
                    new Random().nextInt(10);
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_BANK_CARD) {
            if (data == null)
                return;
            BankCardBean result = (BankCardBean) data.getSerializableExtra("result");
            mTvBankCard.setText(result.getBankName() + "(" + result.getBankNumber() + ")");
            mEntity.setBankCard(result.getBankName() + (mEntity.isReceived() ? "" : "-") + "储蓄卡 (" + result.getBankNumber() + ") ");
        }
    }
}
